const pxToVw = {
  mounted(el) {
    convertPxToVw(el);
  },
  updated(el) {
    convertPxToVw(el);
  },
};

function convertPxToVw(el) {
  const style = el.getAttribute("style");
  if (style && style.includes("px")) {
    const converted = style.replace(/(\d+)px/g, (match, p1) => {
      // 设计稿宽度为 1920px (1920px = 100vw)
      const vwValue = (p1 / 19.2).toFixed(4);
      return `${vwValue}vw`;
    });
    el.setAttribute("style", converted);
  }
}

export default pxToVw;
